<template>
  <el-dialog
    title="修改菜单"
    :visible="showDialog"
    @close="handleClose"
    @open="handelOpened"
  >
    <!-- 表单 -->
    <el-form
      ref="addRef"
      label-width="120px"
      :model="menuData"
      :rules="addFormRules"
      :status-icon="true"
      :hide-required-asterisk="true"
    >
      <el-form-item label="权限组类型">
        <el-radio-group v-model="menuData.is_point">
          <el-radio :label="false">菜单</el-radio>
          <el-radio :label="true">权限点</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限组名称">
        <el-select v-model="menuData.pid" placeholder="请选择权限组名称">
          <el-option
            v-for="item in menusList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="权限代码" prop="code">
        <el-input
          v-model="menuData.code"
          style="width: 80%"
          placeholder="请输入权限代码"
        />
      </el-form-item>
      <el-form-item label="权限标题" prop="title">
        <el-input
          v-model="menuData.title"
          style="width: 80%"
          placeholder="请输入权限标题"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click.stop="handleClose">取消</el-button>
      <el-button type="primary" @click="handleAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { resetMenuAPI } from '@/api/menus'
export default {
  name: 'EditMenus',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    menusList: {
      type: Array,
      required: true
    },
    menuData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      // 表单校验规则
      addFormRules: {
        code: [
          {
            required: true,
            message: '请输入权限代码，此信息必须填写',
            trigger: 'blur'
          }
        ],
        title: [
          {
            required: true,
            message: '请输入权限标题，此信息必须填写',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.handelOpened()
  },
  methods: {
    // 弹窗打开时
    async handelOpened() {
      console.log('openEditmenus')
      // const resp = await getMenuInfoAPI(this.menuData)
      // console.log(resp)
    },
    // 关闭弹窗
    async handleClose() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return
        // await this.$confirm(
        //   '此操作会清空已编辑数据，确定退出编辑吗？',
        //   '温馨提示',
        //   {
        //     type: 'warning'
        //   }
        // )
        this.$emit('update:showDialog', false)
        await this.$refs.addRef.resetFields()
      })
    },
    async handleAdd() {
      try {
        // 整体校验
        // 发起新增请求
        await resetMenuAPI(this.menuData)
        // 获取员工列表
        //   this.$parent.getEmpioyeeList()
        //  推荐子传父
        this.$emit('add-success')
        //   成功提示
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
      } finally {
        // 关闭弹框
        this.handleClose()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
